package MyStackDemo;

import java.util.Scanner;
import java.util.Stack;

/**
 * 方法描述:利用栈的特性判断括号匹配问题
 * @since: 1.14.1
 * @param:
 * @return:
 * @author: 沈新源
 * @date: 2021/4/6 0006
 */
public class test01 {

    public static void fun(char []arr){
        Stack<Character> st = new Stack<>();

        for (char c : arr) {
            if (transversal(c) <= 100)
                st.push(c);
            if (transversal(c) > 100 && (transversal(st.peek()) + transversal(c) == 201))
                st.pop();
        }
        if(st.isEmpty())
            System.out.println("所有括号匹配！");
        else {
            System.out.println(st.toString());
            System.out.println("存在不匹配括号！");
        }
    }

    public static int transversal(char c){
        if(c == '(')
            return 1;
        if(c == '{')
            return 2;
        if(c == '[')
            return 3;
        if(c == '（')
            return 4;
        if(c == '【')
            return 5;

        if(c == '】')
            return 196;
        if(c == '）')
            return 197;
        if(c == ']')
            return 198;
        if(c == '}')
            return 199;
        if(c == ')')
            return 200;

        return -1;
    }



    public static void main(String[] args) {
        System.out.println("请输入一段字符：");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();

        fun(s.toCharArray());
    }
}
